package com.dong.web.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3p0Util {

	static Map<String, ComboPooledDataSource> pooled = new HashMap<String, ComboPooledDataSource>();

	static {
		System.out.println("C3p0Util init...");
		List<String> list = ConfigUtil.dom4jUtil();
		for (String name : list) {
			pooled.put(name, new ComboPooledDataSource(name));
		}
		System.out.println(pooled);
	}

	public static Connection getConnection(String key) {
		try {
			return pooled.get(key).getConnection();
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void close(Connection conn, PreparedStatement prepared,
			ResultSet rs) {
		try {
			if (rs != null) {
				rs.close();
				rs = null;
			}
			if (prepared != null) {
				prepared.close();
				prepared = null;
			}
			if (conn != null) {
				conn.close();
				conn = null;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}
